from extensions import db
from werkzeug.security import generate_password_hash, check_password_hash


class Supplier(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    short_name = db.Column(db.String(100))
    address = db.Column(db.String(255))
    phone = db.Column(db.String(50))
    email = db.Column(db.String(100))
    contact = db.Column(db.String(100))
    contact_phone = db.Column(db.String(50))
    remark = db.Column(db.Text)

    def to_dict(self):
        return {
            '客户编号': self.id,
            '客户名称': self.name,
            '客户简称': self.short_name,
            '地址': self.address,
            '公司电话': self.phone,
            '邮件': self.email,
            '联系人': self.contact,
            '联系人电话': self.contact_phone,
            '备注': self.remark,
        }


class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    price = db.Column(db.Float, nullable=False)
    supplier_id = db.Column(db.Integer, db.ForeignKey('supplier.id'), nullable=False)
    description = db.Column(db.Text)
    remark = db.Column(db.Text)

    supplier = db.relationship('Supplier', backref=db.backref('products', lazy=True))

    def to_dict(self):
        return {
            '商品编号': self.id,
            '商品名称': self.name,
            '商品单价': self.price,
            '供应商编号': self.supplier_id,
            '商品简介': self.description,
            '备注': self.remark,
        }


class Employee(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    level = db.Column(db.Integer, default=1)
    phone = db.Column(db.String(20))
    salary = db.Column(db.Float)
    remark = db.Column(db.Text)

    def to_dict(self):
        return {
            '员工编号': self.id,
            '员工姓名': self.name,
            '员工密码': self.password,
            '员工级别': self.level,
            '员工电话': self.phone,
            '员工工资': self.salary,
            '备注': self.remark,
        }


class PurchaseMaster(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    employee_id = db.Column(db.Integer, db.ForeignKey('employee.id'))
    quantity = db.Column(db.Integer)
    total_price = db.Column(db.Numeric(10, 2))
    purchase_time = db.Column(db.DateTime)
    remark = db.Column(db.Text)

    employee = db.relationship('Employee', backref=db.backref('purchases', lazy=True))

    def __init__(self, id=None, employee_id=None, quantity=None, total_price=None, purchase_time=None, remark=None):
        if id:
            self.id = id
        self.employee_id = employee_id
        self.quantity = quantity
        self.total_price = total_price
        self.purchase_time = purchase_time
        self.remark = remark

    def to_dict(self):
        return {
            '采购清单号': self.id,
            '员工编号': self.employee_id,
            '采购数量': self.quantity,
            '采购总价': self.total_price,
            '采购时间': self.purchase_time.isoformat(),
            '备注': self.remark,
            '细节': [detail.to_dict() for detail in self.details]
        }


class PurchaseDetail(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    list_id = db.Column(db.Integer, db.ForeignKey('purchase_master.id'))
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
    quantity = db.Column(db.Integer)
    unit_price = db.Column(db.Numeric(10, 2))
    total_price = db.Column(db.Numeric(10, 2))
    remark = db.Column(db.Text)

    purchase_master = db.relationship('PurchaseMaster', backref=db.backref('details', lazy=True))
    product = db.relationship('Product', backref=db.backref('purchases', lazy=True))

    def __init__(self, id=None, list_id=None, product_id=None, quantity=None, unit_price=None, total_price=None, remark=None):
        if id:
            self.id = id
        self.list_id = list_id
        self.product_id = product_id
        self.quantity = quantity
        self.unit_price = unit_price
        self.total_price = total_price
        self.remark = remark

    def to_dict(self):
        return {
            '采购明细号': self.id,
            '采购清单号': self.list_id,
            '商品编号': self.product_id,
            '采购数量': self.quantity,
            '商品单价': self.unit_price,
            '商品总价': self.total_price,
            '备注': self.remark,
        }


class Admin(db.Model):
    username = db.Column(db.String(50), primary_key=True)
    password = db.Column(db.String(255), nullable=False)

    def __init__(self, username, password):
        self.username = username
        self.password = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password, password)

    @property
    def is_active(self):
        return True

    def get_id(self):
        return self.username

    @property
    def is_authenticated(self):
        return True

    @property
    def is_anonymous(self):
        return False
